<?php

namespace app\customer\controller\audit\rongcui;

use app\rongcui\model\CustomerNatureModel;
use app\rongcui\model\RongcuiBuyerModel;
use app\rongcui\model\RongcuiIndustryCateModel;

class BuyerController extends \app\rongcui\controller\BuyerController
{
    function index() {
        $where = [];
        if (input('start_time')) {
            $where['buyer.create_time'] = ['gt', strtotime(input('start_time'))];
        }
        if (input('end_time')) {
            $endtime = strtotime(input('end_time'));
            $datetime = strtotime(date('Y-m-d', $endtime));
            if ($endtime == $datetime) {
                $endtime += 86399;
            }
            if (isset($where['buyer.create_time'])) {
                $where['buyer.create_time'] = ['BETWEEN', [strtotime(input('start_time')), $endtime]];
            } else {
                $where['buyer.create_time'] = ['lt', $endtime];
            }
        }
        if (input('keyword')) {
            $keyword = input('keyword');
            $where['user.user_nickname'] = ['like', "%$keyword%"];
        }

        $buyerModel = new RongcuiBuyerModel();
        $lists = $buyerModel
            ->alias('buyer')
            ->join('__RONGCUI_BUYER_USER__ buser', 'buyer.id = buser.pid and buser.level = 1')
            ->join('__USER__ user', 'buser.uid = user.id')
            ->join('__RONGCUI_INDUSTRY_TYPE__ ctype', 'buyer.id = ctype.pid')
            ->join('__CUSTOMER_NATURE__ nature', 'buyer.nature = nature.id','left')
            ->group('buyer.id')
            ->where('char_length(buyer.name) <= 8')
            ->where($where)
            ->field([
                'buyer.*',
                'nature.name nname',
                'user.user_nickname'
            ])
            ->order('buyer.createtime desc,buyer.id desc')
            ->paginate();


        $where_r = input();
        $lists->appends($where_r);
        $page = $lists->render();

        $this->assign('lists', $lists);
        $this->assign('where_r', $where_r);
        $this->assign('page', $page);
        return $this->fetch();
    }
}